home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 101-125 / disk_105 / bison / lex.c < prev    next >
C/C++ Source or Header  |  1992-05-06  |  8KB  |  407 lines

  1. /* Token-reader for Bison's input parser,
  2.    copyright (C) 1984 Bob Corbett and Richard Stallman
  3.  
  4.    Permission is granted to anyone to make or distribute verbatim copies of this program
  5.    provided that the copyright notice and this permission notice are preserved;
  6.    and provided that the recipient is not asked to waive or limit his right to
  7.    redistribute copies as permitted by this permission notice;
  8.    and provided that anyone possessing an executable copy
  9.    is granted access to copy the source code, in machine-readable form,
  10.    in some reasonable manner.
  11.  
  12.    Permission is granted to distribute derived works or enhanced versions of
  13.    this program under the above conditions with the additional condition
  14.    that the entire derivative or enhanced work
  15.    must be covered by a permission notice identical to this one.
  16.  
  17.    Anything distributed as part of a package containing portions derived
  18.    from this program, which cannot in current practice perform its function usefully
  19.    in the absense of what was derived directly from this program,
  20.    is to be considered as forming, together with the latter,
  21.    a single work derived from this program,
  22.    which must be entirely covered by a permission notice identical to this one
  23.    in order for distribution of the package to be permitted.
  24.  
  25.  In other words, you are welcome to use, share and improve this program.
  26.  You are forbidden to forbid anyone else to use, share and improve
  27.  what you give them.   Help stamp out software-hoarding!  */
  28.  
  29. /* 
  30.    lex() is the entry point.  It is called from reader.c.
  31.    It returns one of the token-type codes defined in lex.h.
  32.    When an identifier is seen, the code IDENTIFIER is returned
  33.    and the name is looked up in the symbol table using symtab.c;
  34.    symval is set to a pointer to the entry found.  */
  35.  
  36. #include <stdio.h>
  37. #include <ctype.h>
  38. #include "files.h"
  39. #include "symtab.h"
  40. #include "lex.h"
  41.  
  42.  
  43. extern int lineno;
  44. extern int translations;
  45.  
  46.  
  47. char token_buffer[MAXTOKEN + 1];
  48. extern bucket *symval;
  49. int numval;
  50.  
  51. static int unlexed;        /* these two describe a token to be reread */
  52. static bucket *unlexed_symval;    /* by the next call to lex */
  53.  
  54.  
  55.  
  56. init_lex()
  57. {
  58.   unlexed = -1;
  59. }
  60.  
  61.  
  62.  
  63. int
  64. skip_white_space()
  65. {
  66.   register int c;
  67.   register int inside;
  68.  
  69.   c = getc(finput);
  70.  
  71.   for (;;)
  72.     {
  73.       switch (c)
  74.     {
  75.     case '/':
  76.       c = getc(finput);
  77.       if (c != '*')
  78.         fatal("illegal comment");
  79.  
  80.       c = getc(finput);
  81.  
  82.       inside = 1;
  83.       while (inside)
  84.         {
  85.           if (c == '*')
  86.         {
  87.           while (c == '*')
  88.             c = getc(finput);
  89.  
  90.           if (c == '/')
  91.             {
  92.               inside = 0;
  93.               c = getc(finput);
  94.             }
  95.         }
  96.           else if (c == '\n')
  97.         {
  98.           lineno++;
  99.           c = getc(finput);
  100.         }
  101.           else if (c == EOF)
  102.         fatal("unterminated comment");
  103.           else
  104.         c = getc(finput);
  105.         }
  106.  
  107.       break;
  108.  
  109.     case '\n':
  110.       lineno++;
  111.  
  112.     case ' ':
  113.     case '\t':
  114.     case '\f':
  115.       c = getc(finput);
  116.       break;
  117.  
  118.     default:
  119.       return (c);
  120.     }
  121.     }
  122. }
  123.  
  124.  
  125.  
  126. unlex(token)
  127. int token;
  128. {
  129.   unlexed = token;
  130.   unlexed_symval = symval;
  131. }
  132.  
  133.  
  134.  
  135. int
  136. lex()
  137. {
  138.   register int c;
  139.   register char *p;
  140.  
  141.   if (unlexed >= 0)
  142.     {
  143.       symval = unlexed_symval;
  144.       c = unlexed;
  145.       unlexed = -1;
  146.       return (c);
  147.     }
  148.  
  149.   c = skip_white_space();
  150.  
  151.   switch (c)
  152.     {
  153.     case EOF:
  154.       return (ENDFILE);
  155.  
  156.     case 'A':  case 'B':  case 'C':  case 'D':  case 'E':
  157.     case 'F':  case 'G':  case 'H':  case 'I':  case 'J':
  158.     case 'K':  case 'L':  case 'M':  case 'N':  case 'O':
  159.     case 'P':  case 'Q':  case 'R':  case 'S':  case 'T':
  160.     case 'U':  case 'V':  case 'W':  case 'X':  case 'Y':
  161.     case 'Z':
  162.     case 'a':  case 'b':  case 'c':  case 'd':  case 'e':
  163.     case 'f':  case 'g':  case 'h':  case 'i':  case 'j':
  164.     case 'k':  case 'l':  case 'm':  case 'n':  case 'o':
  165.     case 'p':  case 'q':  case 'r':  case 's':  case 't':
  166.     case 'u':  case 'v':  case 'w':  case 'x':  case 'y':
  167.     case 'z':
  168.     case '.':  case '_':
  169.       p = token_buffer;
  170.       while (isalnum(c) || c == '_' || c == '.')
  171.     {
  172.       if (p < token_buffer + MAXTOKEN)
  173.         *p++ = c;
  174.       c = getc(finput);
  175.     }
  176.  
  177.       *p = 0;
  178.       ungetc(c, finput);
  179.       symval = getsym(token_buffer);
  180.       return (IDENTIFIER);
  181.  
  182.     case '0':  case '1':  case '2':  case '3':  case '4':
  183.     case '5':  case '6':  case '7':  case '8':  case '9':
  184.       {
  185.     numval = 0;
  186.  
  187.     while (isdigit(c))
  188.       {
  189.         numval = numval*10 + c - '0';
  190.         c = getc(finput);
  191.       }
  192.     ungetc(c, finput);
  193.     return (NUMBER);
  194.       }
  195.  
  196.     case '\'':
  197.       translations = -1;
  198.  
  199.       /* parse the literal token and compute character code in  code  */
  200.  
  201.       c = getc(finput);
  202.       {
  203.     register int code = 0;
  204.  
  205.     if (c == '\\')
  206.       {
  207.         c = getc(finput);
  208.  
  209.         if (c <= '7' && c >= '0')
  210.           {
  211.         while (c <= '7' && c >= '0')
  212.           {
  213.             code = (code * 8) + (c - '0');
  214.             c = getc(finput);
  215.           }
  216.         if (c >= 128)
  217.           fatal("malformatted literal token");
  218.           }
  219.         else
  220.           {
  221.         if (c == 't')
  222.           code = '\t';
  223.         else if (c == 'n')
  224.           code = '\n';
  225.         else if (c == 'r')
  226.           code = '\r';
  227.         else if (c == 'f')
  228.           code = '\f';
  229.         else if (c == 'b')
  230.           code = '\b';
  231.         else if (c == '\\')
  232.           code = '\\';
  233.         else if (c == '\'')
  234.           code = '\'';
  235.         else fatal("invalid literal token");
  236.         c = getc(finput);
  237.           }
  238.       }
  239.     else
  240.       {
  241.         code = c;
  242.         c = getc(finput);
  243.       }
  244.     if (c != '\'')
  245.       fatal("malformatted literal token");
  246.  
  247.     /* now fill token_buffer with the canonical name for this character
  248.        as a literal token.  Do not use what the user typed,
  249.        so that '\012' and '\n' can be interchangeable.  */
  250.  
  251.     p = token_buffer;
  252.     *p++ = '\'';
  253.     if (code == '\\')
  254.       {
  255.         p = token_buffer + 1;
  256.         *p++ = '\\';
  257.         *p++ = '\\';
  258.       }
  259.     else if (code == '\'')
  260.       {
  261.         p = token_buffer + 1;
  262.         *p++ = '\\';
  263.         *p++ = '\'';
  264.       }
  265.     else if (code >= 040 && code != 0177)
  266.       *p++ = code;
  267.     else if (code == '\t')
  268.       {
  269.         p = token_buffer + 1;
  270.         *p++ = '\\';
  271.         *p++ = 't';
  272.       }
  273.     else if (code == '\n')
  274.       {
  275.         p = token_buffer + 1;
  276.         *p++ = '\\';
  277.         *p++ = 'n';
  278.       }
  279.     else if (code == '\r')
  280.       {
  281.         p = token_buffer + 1;
  282.         *p++ = '\\';
  283.         *p++ = 'r';
  284.       }
  285.     else if (code == '\b')
  286.       {
  287.         p = token_buffer + 1;
  288.         *p++ = '\\';
  289.         *p++ = 'b';
  290.       }
  291.     else if (code == '\f')
  292.       {
  293.         p = token_buffer + 1;
  294.         *p++ = '\\';
  295.         *p++ = 'f';
  296.       }
  297.         else
  298.       {
  299.         *p++ = code / 0100 + '0';
  300.         *p++ = ((code / 010) & 07) + '0';
  301.         *p++ = (code & 07) + '0';
  302.       }
  303.     *p++ = '\'';
  304.     *p = 0;
  305.     symval = getsym(token_buffer);
  306.     symval->class = STOKEN;
  307.     if (! symval->user_token_number)
  308.       symval->user_token_number = code;
  309.     return (IDENTIFIER);
  310.       }
  311.  
  312.     case ',':
  313.       return (COMMA);
  314.  
  315.     case ':':
  316.       return (COLON);
  317.  
  318.     case ';':
  319.       return (SEMICOLON);
  320.  
  321.     case '|':
  322.       return (BAR);
  323.  
  324.     case '{':
  325.       return (LEFT_CURLY);
  326.  
  327.     case '<':
  328.       p = token_buffer;
  329.       c = getc(finput);
  330.       while (c != '>')
  331.     {
  332.       if (c == '\n' || c == EOF)
  333.         fatal("unterminated type name");
  334.  
  335.       if (p >= token_buffer + MAXTOKEN - 1)
  336.         fatal("type name too long");
  337.  
  338.       *p++ = c;
  339.       c = getc(finput);
  340.     }
  341.       *p = 0;
  342.       return (TYPENAME);
  343.         
  344.  
  345.     case '%':
  346.       return (parse_percent_token());
  347.  
  348.     default:
  349.       return (ILLEGAL);
  350.     }
  351. }
  352.  
  353.  
  354. /* parse a token which starts with %.  Assumes the % has already been read and discarded.  */
  355.  
  356. int
  357. parse_percent_token ()
  358. {
  359.   register int c;
  360.   register char *p;
  361.  
  362.   p = token_buffer;
  363.   c = getc(finput);
  364.  
  365.   if (c == '%')
  366.     return (TWO_PERCENTS);
  367.   if (c == '{')
  368.     return (PERCENT_LEFT_CURLY);
  369.   if (isalpha(c)) {}
  370.   else return (ILLEGAL);
  371.  
  372.   while (isalpha(c) || c == '_')
  373.     {
  374.       if (p < token_buffer + MAXTOKEN)
  375.     *p++ = c;
  376.       c = getc(finput);
  377.     }
  378.  
  379.   ungetc(c, finput);
  380.  
  381.   *p = 0;
  382.  
  383.   if (strcmp(token_buffer, "token") == 0)
  384.     return (TOKEN);
  385.   else if (strcmp(token_buffer, "nterm") == 0)
  386.     return (NTERM);
  387.   else if (strcmp(token_buffer, "type") == 0)
  388.     return (TYPE);
  389.   else if (strcmp(token_buffer, "guard") == 0)
  390.     return (GUARD);
  391.   else if (strcmp(token_buffer, "union") == 0)
  392.     return (UNION);
  393.   else if (strcmp(token_buffer, "start") == 0)
  394.     return (START);
  395.   else if (strcmp(token_buffer, "left") == 0)
  396.     return (LEFT);
  397.   else if (strcmp(token_buffer, "right") == 0)
  398.     return (RIGHT);
  399.   else if (strcmp(token_buffer, "semantic_parser") == 0)
  400.     return (SEMANTIC_PARSER);
  401.   else if (strcmp(token_buffer, "pure_parser") == 0)
  402.     return (PURE_PARSER);
  403.   else if (strcmp(token_buffer, "prec") == 0)
  404.     return (PREC);
  405.   else return (ILLEGAL);
  406. }
  407.